package cn.rkylin.oms.salespromotion.rules.vo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.rkylin.oms.salespromotion.rules.domain.SalesPromotionConstrainGoods;
import cn.rkylin.oms.salespromotion.rules.domain.SalesPromotionGift;
import cn.rkylin.oms.salespromotion.rules.domain.SalesPromotionRule;
import cn.rkylin.oms.salespromotion.rules.domain.SalesPromotionShop;

public class SalesPromotionRuleVO extends SalesPromotionRule {
    private static final long serialVersionUID = 3979870289705373989L;

    public static String VALUE_YES = "y";
    public static String VALUE_NO = "n";

    public String COUNTING_DATE_TYPE_PAY_TIME = "0";
    public String COUNTING_DATE_TYPE_ORDER_TIME = "1";

    /**
     * 查询条件
     */
    private String searchCondition;
    /**
     * 排序规则
     */
    private String orderBy;

    /**
     * 促销规则相关店铺列表
     */
    private List<SalesPromotionShop> shopList;
    /**
     * 促销规则相关赠品列表
     */
    private List<SalesPromotionGift> giftList;
    /**
     * 促销规则相关限制商品列表
     */
    private List<SalesPromotionConstrainGoods> constrainGoodsList;

    public List<SalesPromotionShop> getShopList() {
        return shopList;
    }

    public void setShopList(List<SalesPromotionShop> shopList) {
        this.shopList = shopList;
    }

    public List<SalesPromotionGift> getGiftList() {
        return giftList;
    }

    public void setGiftList(List<SalesPromotionGift> GiftList) {
        this.giftList = GiftList;
    }

    public List<SalesPromotionConstrainGoods> getConstrainGoodsList() {
        return constrainGoodsList;
    }

    public void setConstrainGoodsList(List<SalesPromotionConstrainGoods> constrainGoodsList) {
        this.constrainGoodsList = constrainGoodsList;
    }

    public String getSearchCondition() {
        return searchCondition;
    }

    public void setSearchCondition(String searchCondition) {
        this.searchCondition = searchCondition;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    // 编辑-DISABLED
    private static final String OPERATION_BTN_EDIT_DISABLED = "<button id=\"%s\" type=\"button\" disabled class=\"btn btn-disable  btn-xs\" onclick='salespromotionrule.editRule(this)'><i class=\"fa fa-edit\"></i>&nbsp;修改</button>&nbsp";
    // 编辑
    private static final String OPERATION_BTN_EDIT = "<button id=\"%s\" type=\"button\" class=\"btn btn-info btn-xs\" onclick='salespromotionrule.editRule(this)'><i class=\"fa fa-edit\"></i>&nbsp;修改</button>&nbsp";

    // 启用
    private static final String OPERATION_BTN_ENABLE = "<button id=\"%s\" type=\"button\" class=\"btn btn-success btn-xs\" onclick='salespromotionrule.enableRule(this)'><i class=\"fa fa-power-off\"></i>&nbsp;启用</button>&nbsp";
    private static final String OPERATION_BTN_VIEW = "<button id=\"%s\" type=\"button\" class=\"btn btn-info btn-xs\" onclick='salespromotionrule.viewRule(this)'><i class=\"fa fa-list-alt\"></i>&nbsp;查看</button>&nbsp";
    // 禁用
    private static final String OPERATION_BTN_DISABLE = "<button id=\"%s\" type=\"button\" class=\"btn btn-danger btn-xs\" onclick='salespromotionrule.disableRule(this)'><i class=\"fa fa-power-off\"></i>&nbsp;停用</button>&nbsp";

    // 查看赠送记录
    private static final String OPERATION_BTN_Gift_HISTORY = "<button  id=\"%s\"type=\"button\" class=\"btn btn-info btn-xs\" onclick='salespromotionrule.queryGiftHistroy(this)'><i class=\"fa fa-list-alt\"></i>&nbsp;查看赠送记录</button>&nbsp";
    // 删除
    private static final String OPERATION_BTN_DELETE = "<button id=\"%s\" type=\"button\" class=\"btn btn-danger btn-xs\" onclick='salespromotionrule.deleteRule(this)'><i class=\"fa fa-trash-o\"></i>&nbsp;删除</button>&nbsp";
    // 删除-DISABLED
    private static final String OPERATION_BTN_DELETE_DISABLED = "<button id=\"%s\" type=\"button\" disabled class=\"btn btn-disable  btn-xs\" onclick='salespromotionrule.deleteRule(this)'><i class=\"fa fa-trash-o\"></i>&nbsp;删除</button>&nbsp";
    // 查看操作日志
    private static final String OPERATION_BTN_LOG = "<button  id=\"%s\" type=\"button\" class=\"btn btn-info btn-xs\" onclick='salespromotionrule.queryOperationLog(this)'><i class=\"fa fa-list-alt\"></i>&nbsp;查看操作日志</button>&nbsp";
    // 复制
    private static final String OPERATION_BTN_COPY = "<button  id=\"%s\" type=\"button\" class=\"btn btn-info btn-xs\" onclick='salespromotionrule.copyRule(this)'><i class=\"fa fa-copy\"></i>&nbsp;复制</button>&nbsp";

    // 规则状态——开始
    private static final String STATUS_EXPIRE = "<span class='label label-sm label-warning'>过期</span>";
    private static final String STATUS_YES = "<span class='label label-sm label-success'>启用</span>";
    private static final String STATUS_NO = "<span class='label label-sm label-default'>停用</span>";
    // 规则状态——结束

    // 启用状态
    private String status;

    // 规则类型
    private String ruleType;

    public String getRuleType() {
        return ruleType;
    }

    public void setRuleType(String ruleType) {
        String rule = getPresentCaculateRule();

        if (rule.contains("fullSend")) {
            this.ruleType = "满就送";
        } else {
            this.ruleType = "买就送";
        }
    }

    // 操作
    private String operation;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        /*
         * 如果规则已禁用，则显示已禁用。 如果规则已启用，并且当前时间早于规则失效时间，则显示已生效；否则显示已失效。
         */
        if ("n".equalsIgnoreCase(getIsEnable())) {
            this.status = STATUS_NO;
        } else {
            Date now = new Date();
            if (null != getCountingDateEnd()) {
                if (now.getTime() <= getCountingDateEnd().getTime()) {
                    this.status = STATUS_YES;
                } else {
                    this.status = STATUS_EXPIRE;

                }
            } else {
                this.status = STATUS_YES;
            }
        }

    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        Date now = new Date();
        StringBuilder strbuilder = new StringBuilder();

        if ("a".equalsIgnoreCase(getIsEnable())) {
            // 未启用
            strbuilder.append(String.format(OPERATION_BTN_ENABLE, getRuleId())).append(String.format(OPERATION_BTN_EDIT, getRuleId()))
                    .append(String.format(OPERATION_BTN_COPY, getRuleId())).append(String.format(OPERATION_BTN_DELETE, getRuleId()));
        } else if ("y".equals(getIsEnable()) && now.getTime() <= getCountingDateEnd().getTime()) {
            // 启用
            strbuilder.append(String.format(OPERATION_BTN_DISABLE, getRuleId())).append(String.format(OPERATION_BTN_COPY, getRuleId()))
                    .append(String.format(OPERATION_BTN_VIEW, getRuleId()));
        } else if ("n".equals(getIsEnable())) {
            // 停用
            strbuilder.append(String.format(OPERATION_BTN_COPY, getRuleId())).append(String.format(OPERATION_BTN_VIEW, getRuleId()));
        } else if ("y".equals(getIsEnable()) && now.getTime() > getCountingDateEnd().getTime()) {
            // 过期
            strbuilder.append(String.format(OPERATION_BTN_COPY, getRuleId())).append(String.format(OPERATION_BTN_VIEW, getRuleId()));
        }
        //
        //
        // // 如果超期了，只能复制
        // if(now.getTime()>getCountingDateEnd().getTime()) {
        // strbuilder
        // .append(String.format(OPERATION_BTN_DELETE, getRuleId()))
        // .append(String.format(OPERATION_BTN_COPY, getRuleId()))
        // ;
        // }else {
        // if("n".equalsIgnoreCase(getIsEnable())) {
        // strbuilder
        // .append(String.format(OPERATION_BTN_ENABLE, getRuleId()))
        // .append(String.format(OPERATION_BTN_EDIT, getRuleId()))
        // .append(String.format(OPERATION_BTN_COPY, getRuleId()))
        // .append(String.format(OPERATION_BTN_DELETE, getRuleId()))
        // ;
        // }else {
        // strbuilder
        // .append(String.format(OPERATION_BTN_DISABLE, getRuleId()))
        // .append(String.format(OPERATION_BTN_COPY, getRuleId()));
        // }
        // }

        this.operation = strbuilder.toString();
    }

    /**
     * 列表上展现的复选框列
     */
    String checkbox = "";

    public String getCheckbox() {
        return checkbox;
    }

    public void setCheckbox(String checkbox) {
        this.checkbox = "<input name='chkRule' class='chkRule icheck'  style=\"display:none;\"  type='checkbox' id='" + this.getRuleId() + "'/>";
    }

    /**
     * 更新规则时读取此信息 标识店铺是否被前台修改过
     */
    private String shopEditFlag = "n";
    /**
     * 更新规则时读取此信息 标识赠品列表是否被前台修改过
     */
    private String giftEditFlag = "n";
    /**
     * 更新规则时读取此信息 标识限制商品列表是否被前台修改过
     */
    private String constrainGoodsEditFlag = "n";

    public String getShopEditFlag() {
        return shopEditFlag;
    }

    public void setShopEditFlag(String shopEditFlag) {
        this.shopEditFlag = shopEditFlag;
    }

    public String getGiftEditFlag() {
        return giftEditFlag;
    }

    public void setGiftEditFlag(String giftEditFlag) {
        this.giftEditFlag = giftEditFlag;
    }

    public String getConstrainGoodsEditFlag() {
        return constrainGoodsEditFlag;
    }

    public void setConstrainGoodsEditFlag(String constrainGoodsEditFlag) {
        this.constrainGoodsEditFlag = constrainGoodsEditFlag;
    }

    /**
     * 列表上展现的时间类型
     */
    private String countingDateTypeStr;

    public String getCountingDateTypeStr() {
        return countingDateTypeStr;
    }

    public void setCountingDateTypeStr(String countingDateTypeStr) {
        String dateTypeStr = "付款时间";
        if ("0".equalsIgnoreCase(getCountingDateType())) {
            dateTypeStr = "付款时间";
        } else {
            dateTypeStr = "拍单时间";
        }
        this.countingDateTypeStr = dateTypeStr;
    }

    /**
     * 列表上展现的参与活动的店铺列表
     */
    private String envolvingShops;

    public String getEnvolvingShops() {
        return envolvingShops;
    }

    public void setEnvolvingShops(String envolvingShops) {
        String shops = "";
        if (getShopList() != null && getShopList().size() > 0) {
            for (SalesPromotionShop shop : getShopList()) {
                shops = shops.concat(shop.getRelShopName() + ";");
            }
            this.envolvingShops = shops.substring(0, shops.length() - 1);
        } else {
            this.envolvingShops = "";
        }
    }

    /*
     * 过期规则的查询条件
     */
    private String isExpire;

    public String getIsExpire() {
        return isExpire;
    }

    public void setIsExpire(String isExpire) {
        this.isExpire = isExpire;
    }

    // 计算赠品用的查询条件
    /**
     * payTime:付款时间.
     */
    private Date payTime;

    /**
     * orderTime:拍单时间.
     */
    private Date orderTime;

    /**
     * orderAmount:订单金额.
     */
    private BigDecimal orderAmount;

    /**
     * shopId:店铺ID.
     */
    private String shopId;
    // 计算赠品用的查询条件

    public Date getPayTime() {
        return payTime;
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    public Date getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Date orderTime) {
        this.orderTime = orderTime;
    }

    public BigDecimal getOrderAmount() {
        return orderAmount;
    }

    public void setOrderAmount(BigDecimal orderAmount) {
        this.orderAmount = orderAmount;
    }

    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    // 查询条件的构造
    private String querySendType; // 促销方式
    private String queryShopId; // 店铺ID
    private String queryConstrainGoods; // 限制商品
    private String queryGift; // 赠品
    private String queryDateType; // 时间类型
    private Date queryDateStart; // 开始时间
    private Date queryDateEnd; // 结束时间

    public String getQuerySendType() {
        return querySendType;
    }

    public void setQuerySendType(String querySendType) {
        this.querySendType = querySendType;
    }

    public String getQueryShopId() {
        return queryShopId;
    }

    public void setQueryShopId(String queryShopId) {
        this.queryShopId = queryShopId;
    }

    public String getQueryConstrainGoods() {
        return queryConstrainGoods;
    }

    public void setQueryConstrainGoods(String queryConstrainGoods) {
        this.queryConstrainGoods = queryConstrainGoods;
    }

    public String getQueryGift() {
        return queryGift;
    }

    public void setQueryGift(String queryGift) {
        this.queryGift = queryGift;
    }

    public String getQueryDateType() {
        return queryDateType;
    }

    public void setQueryDateType(String queryDateType) {
        this.queryDateType = queryDateType;
    }

    public Date getQueryDateStart() {
        return queryDateStart;
    }

    public void setQueryDateStart(Date queryDateStart) {
        this.queryDateStart = queryDateStart;
    }

    public Date getQueryDateEnd() {
        return queryDateEnd;
    }

    public void setQueryDateEnd(Date queryDateEnd) {
        this.queryDateEnd = queryDateEnd;
    }
}
